package com.apache.uct.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.apache.api.api.ApiService;
import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.exception.ServiceException;
import com.apache.license.validator.LicenseValidate;

/**
 * description: 用户中心统一对外接口实现
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class UctServiceImpl implements ApiService {

	private Logger log = Logger.getLogger(UctServiceImpl.class);
	private Map<String, PluginConnector> plugins = new HashMap<String, PluginConnector>();

	/**
	 * TODO vo 方法参数<br>
	 *      beanId:uctService<br>
	 *      方法标识：<br>
	 *          userList：用户列表查询;orgList：机构列表查询 ;deptList:部门列表查询;roleList:角色列表查询<br>
	 *          actList:权限列表查询;lockList:资源列表查询 ; userEntity:按属性查询单个用户信息;<br>
	 *          orgEntity:按属性查询单个机构信息; deptEntity:按属性查询单个部门信息 <br>
	 *          actEntity:按属性查询单个权限信息;grantActToUser:给用户授权;<br>
	 *          unitySystemConfig:获取纺一配置信息内容;loginUserInfoSso:获取用户登录信息<br>
	 *          flowCore:工作流系统特殊操作处理;saveUser：新增修改用户
	 */
	public ResultEntity doService(ParamsVo vo) throws ServiceException {
		ResultEntity result = new ResultEntity();
		result.setResult("false");
		if (Validator.isEmpty(plugins.get(vo.getMethodKey()))) {
			result.setMessage("没有确要执行的方法");
		} else {
			try {
				if (LicenseValidate.getInstance().islasttime()) {//验证系统license
					String sysPass = String.valueOf(vo.getParams("sysPass"));
					if (SystemTools.getInstance().getValue("push_to_dwusoft_pass").equals(sysPass)) {//合法身份校验
						ResultEntity entity = (ResultEntity) plugins.get(vo.getMethodKey()).execute(vo);
						return entity;
					} else {
						result.setMessage("非法访问");
					}
				} else {
					result.setMessage("功能已停用或已删除");
					log.error("系统license已过期,请重新申请license文件");
				}
			} catch (Exception e) {
				log.error(e);
				throw new ServiceException("执行方法[" + vo.getMethodKey() + "]", e);
			}
		}
		return result;
	}

	public void setPlugins(Map<String, PluginConnector> plugins) {
		this.plugins = plugins;
	}
}
